package model;

import java.util.HashSet;

/**
 *
 * @author Mads Nielsen
 *
 */
public class CustomerCollection {

    private static CustomerCollection instance;
    private HashSet<Customer> customers;

    private CustomerCollection() {
        customers = new HashSet<Customer>();
    }

    public static CustomerCollection getInstance() {
        if (!(instance instanceof CustomerCollection)) {
            instance = new CustomerCollection();
        }

        return instance;
    }

    public void addCustomer(Customer customer) throws DuplicateCustomerIdentifierException {

        Customer lookup = getCustomer(customer.getPhone());

        if (lookup != null) {
            throw new DuplicateCustomerIdentifierException();
        }

        customers.add(customer);
    }

    public Customer[] getCustomers() {
        return customers.toArray(new Customer[customers.size()]);
    }

    /**
     * Fetches customer object by phone number
     *
     * @param phone
     *            Phone number to match against
     * @return Matching customer object or null if none found
     */
    public Customer getCustomer(int phone) {
        for (Customer customer : customers) {
            if (customer.getPhone() == phone) {
                return customer;
            }
        }
        return null;
    }
}
